package cg.modelo;

import java.util.ArrayList;

public class Deck implements CardContainer{

	private ArrayList<Card> cards;
	private boolean firstDraw;

	public Deck() {
		this.createDeck(40);
		this.firstDraw = true;
	}
	
	public Deck(int size) {
		if(size <=0)
			size = 40;
		this.createDeck(size);
		this.firstDraw = true;
	}

	private void createDeck(int size) {
		this.cards = new ArrayList<Card>(size);
		for(int i=0; i<size; i++) {
			this.cards.add(new MonsterCard("A1"));
		}

	}

	public Card draw() {
		if((!this.empty()) && (!this.firstDraw())) {
			return this.cards.remove(0);
		}
		//agregar excepcion
		return null;
	}

	private boolean firstDraw() {
		return this.firstDraw;
	}

	private boolean empty() {
		return (this.size() == 0);
	}

	public int size() {
		return this.cards.size();
	}

	public ArrayList<Card> initialDraw() {
		if(this.firstDraw()) {
			this.firstDraw = false;
			ArrayList<Card> initDrawCards = new ArrayList<Card>(6);
			for(int i=0; i<6; i++) {
				Card card = this.draw();
				initDrawCards.add(card);
			}
				
			return initDrawCards;
		}
		//agregar excepcion
		return null;
	}

	public void add(Card card) {
		this.cards.add(card);
	}

	public void add(ArrayList<Card> cards) {
		for(Card card: cards)
			this.add(card);
	}

	public Card removeCard() {
		if(!this.empty())
			return this.cards.remove(0);
		return null;
	}

	public ArrayList<Card> removeCards(int quantity) {
		if(!this.empty()) {
			ArrayList<Card> removeCard = new ArrayList<Card>(quantity);
			for(int i=0; i<quantity; i++) {
				removeCard.add(this.removeCard());
			}
			return removeCard;
		}
		//agregar excepcion
		return null;
	}

	public ArrayList<Card> draw(int quantity) {
		return this.removeCards(quantity);
	}

	public Card removeCard(Card card) {
		if(this.cards.contains(card)) {
			int index = this.cards.indexOf(card);
			return this.cards.remove(index);
		}
		return null;
	}

	public ArrayList<Card> removeAllCards() {
		return this.removeCards(this.size());
	}

	public boolean hasCard(Card card) {
		return this.cards.contains(card);
	}

}
